package sanrenxing.dayEnd.backRepertoryTotal.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sanrenxing.dayEnd.backRepertoryTotal.dao.TransactionDao;
import sanrenxing.dayEnd.backRepertoryTotal.domain.ReceivableAndPayable;
import sanrenxing.dayEnd.backRepertoryTotal.service.SecuritiesService;
import sanrenxing.dayEnd.backRepertoryTotal.service.TransactionService;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desrciption 证券应收应付 核心控制器类
 * @auth：yidu 李小锋
 * @date:2021/3/14 0014 - 10:12
 * @version:1:0
 */
@RestController//风格
@RequestMapping("/securcontroller")
public class SecuritiesController {
    //自动装配
    @Autowired
    //调用证券应收应付业务层对象
    private SecuritiesService securitiesService;
    //调用证券交易数据中的接口
    //自动装配
    @Autowired
    private TransactionService transactionService;
    //调用现金账户
    //自动装配
    @Autowired
    private TransactionDao transactionDao;
    /**
     * 分页查询
     * @param req 请求对象
     * @param page 页数
     * @param limit 行数
     * @return 返回map集合从后端填充数据到前端
     */
    @RequestMapping("/securities")
    public Map<String,Object> selectSecurities(HttpServletRequest req, int page, int limit){
        //创建一个对象用于保存条件ReceivableAndPayable
        ReceivableAndPayable securities = new ReceivableAndPayable();
        //接收来自请求中的业务日期数据
        String businDate = req.getParameter("businDate");
        //判断业务日期不等于空切不为零
        if(businDate!=null && businDate.trim().length()!=0){
            //创建一个日期格式转换
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            //将请求中的日期保存到对象中
            try {
                //将请求中的进行格式对象封装到行情数据对象
                securities.setBusinDate(sdf.parse(businDate));
            } catch (ParseException e) {
                //异常信息打印
                e.printStackTrace();
            }
        }
        //调用业务条件方法
        String getcondition = securitiesService.getcondition(securities);
        //调用分页查询
        List<ReceivableAndPayable> securitiesList = securitiesService.findByPage(limit,page,  getcondition);
        //调用统计行数
        int count = securitiesService.count(getcondition);
        //创建一个map集合用于向前端发送数据
        HashMap<String, Object> hashMap = new HashMap<>();
        //在映射集合中添加key为code，值为0的状态码
        hashMap.put("code",0);
        //在映射集合中添加key为msg,值为响应信息字符串
        hashMap.put("msg","");
        //在映射集合中添加key为count,值总行数的数据对
        hashMap.put("count",count);
        //在映射集合中添加key为data,值为员集合对象
        hashMap.put("data",securitiesList);
        //调用基金findFund
        hashMap.put("fund",transactionService.findFund());
        //调用证券SecuritiesInfomationLX
        hashMap.put("securi",transactionService.SecuritiesInfomationLXF());
        //调用现金账户queryCash
        hashMap.put("cashAccount",transactionDao.queryCash());
        //向前端发送数据
        return hashMap;
    }
    /**
     * 新增方法
     * @param securities 对象
     * @return 0,failure 1，SUCCESS
     */
    @PostMapping("/securities")
    public String add(@RequestBody ReceivableAndPayable securities){
        //获取修改的行数
        int rows = securitiesService.add(securities);
        //使用三元运算符判断
        return rows>0?"add SUCCESS":"add failure";
    }
    /**
     * 删除方法
     * @param srapId 证券编号
     * @return 0,failure 1，SUCCESS
     */
    @DeleteMapping("/securitis/{srapId}")
    public String delete(@PathVariable("srapId") String  srapId){
        //获取修改的行数
        int rows = securitiesService.delete(srapId);
        //使用三元运算符判断
        return rows>0?"delete SUCCESS":"delete failure";
    }
    /**
     * 表示权益中的删除
     * req 请求对象
     * @return
     */
    @PostMapping("/EquityDelete")
    public String  EquityDateDelete(HttpServletRequest req){
        //接收来自请求中的seid
        String seId = req.getParameter("seId");
        //接收来自请求中的businDate
        String businDate = req.getParameter("businDate");
        //定义一个日期格式转换sdf
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            //返回return 调用权益数据删除方法EquityDate)seid busindate
            return securitiesService.EquityDateDelete(seId,sdf.parse(businDate))>0?"success":"failure";
        } catch (ParseException e) {
            //异常信息打印
            e.printStackTrace();
        }
        //返回空
        return "";
    }
    /**
     * 表示证券交易数据中的删除
     * @return
     */
    @PostMapping("/TransDelete")
    public String  TransactionDelete(HttpServletRequest req){
        //接收来自请求中的caId
        String caId = req.getParameter("caId");
        //接收来自请求中的tranDate
        String tranDate = req.getParameter("tranDate");
        //定义一个日期格式转换sdf
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            //调用证券交易数据中的删除Transaction）caId tranDate
            return securitiesService.TransactionDelete(caId,sdf.parse(tranDate))>0?"success":"failure";
        } catch (ParseException e) {
            //异常信息打印
            e.printStackTrace();
        }
        //返回空
        return  "";
    }
    /**
     * 修改方法方法
     * @param securities 对象
     * @return 0,failure 1，SUCCESS
     */
    @PutMapping("/securities")
    public String update(@RequestBody ReceivableAndPayable securities){
        //获取修改的行数
        int rows = securitiesService.update(securities);
        //使用三元运算符判断
        return rows>0?"update SUCCESS":"update failure";
    }

    /**
     * 批量删除
     * @param tranIdStr 编号字符串
     * @return 信息
     */
    @PutMapping("/batchBel/{tranIdStr}")
    public String batchBeleteById(@PathVariable("tranIdStr") String tranIdStr){
        //获取编号集合
        String[] split = tranIdStr.split(",");
        //创建一个返回的字符串
        String result="batchBelete Success";
        //循环遍历删除
        for (String s : split) {
            //进行删除
            int delete = securitiesService.delete(s);
            //判断是否小于等于零
            if (delete<=0){
                //返回错误信息
                return "batchBelete failure";
            }
        }
        //返回信息
        return result;
    }

    /**
     * 证券应收应付对冲销
     * @return
     */
    @DeleteMapping("/method")
    public String washDelete(){
        //调用对冲销方法washDelete
        return securitiesService.washDelete()?"success":"fail";
    }
}
